package zxing.android;

import android.graphics.Bitmap;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;

import java.util.Hashtable;

/**
 * Created by chengww on 2017/3/10.
 */

public class ZUtils {
    /**
     * 解析二维码图片,返回结果封装在Result对象中
     *
     * @param bitmap bitmap
     * @return result
     */
    public static Result parseQRcodeBitmap(Bitmap bitmap) {
        //解析转换类型UTF-8
        Hashtable<DecodeHintType, String> hints = new Hashtable<>();
        hints.put(DecodeHintType.CHARACTER_SET, "utf-8");

        //新建一个RGBLuminanceSource对象，将bitmap图片传给此对象
        MyRGBLuminanceSource rgbLuminanceSource = new MyRGBLuminanceSource(bitmap);
        //将图片转换成二进制图片
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(rgbLuminanceSource));
        //初始化解析对象
        QRCodeReader reader = new QRCodeReader();
        //开始解析
        Result result = null;
        try {
            result = reader.decode(binaryBitmap, hints);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}
